﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Subgurim.Controles;

namespace BitPortal.Modules.UploadFile
{
    public partial class BasicUploadFile : System.Web.UI.UserControl
    {
        /// <summary>
        /// Kich thuoc file cho phep (byte)
        /// </summary>
        public int MaxFileLength = 2048;
        /// <summary>
        /// Dinh dang file cho phep cach nhau boi dau ','
        /// </summary>
        public string AllowTypes = "All";
        /// <summary>
        /// Duoi mo rong cho phep cach nhau boi dau ','
        /// </summary>
        public string AllowExtentions = "All";

        public List<HttpPostedFileAJAX> UploadedFiles { get { return FileUploaderAJAX1.historial; } }

        public FileUploaderAJAX FileUpload { get { return this.FileUploaderAJAX1; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (FileUploaderAJAX1.IsPosting)
                this.managePost();
        }

        private void managePost()
        {
            HttpPostedFileAJAX pf = FileUploaderAJAX1.PostedFile;
            bool flag = false;
            foreach (string str in (AllowTypes + ",").Split(','))
            {
                if (str.Equals("All") || ((!string.IsNullOrEmpty(str)) && pf.ContentType.Contains(str)))
                {
                    flag = true;
                }
            }
            if (!flag)
            {
                foreach (string str in (AllowExtentions + ",").Split(','))
                {
                    if (str.Equals("All") || ((!string.IsNullOrEmpty(str)) && pf.FileName.EndsWith(str, StringComparison.OrdinalIgnoreCase)))
                    {
                        flag = true;
                    }
                }
            }
            if (flag && pf.ContentLength <= MaxFileLength * 1024)
            {
                FileUploaderAJAX1.SaveAs("~/Upload/UserUpload/" + DateTime.Now.Year + "/" + DateTime.Now.Month, pf.FileName);
            }
        }
    }
}